import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.*;
import javax.swing.*;
import com.sun.opengl.util.*;

//Name: Victoria Metzger and Qinlan Shen
//Date: November 8th, 2010
//Class: Software Design Period 8
//Teacher: Collins

public class PuzzlePalooza extends JPanel implements MouseListener, MouseMotionListener
{
	JLabel question = new JLabel();
	char selected = ' ';
	
	int currentX = 0, currentY = 0;
	
	JButton enter = new JButton("Enter!");
	Image option1, option2, option3, option4, select1, select2, select3, select4, offscreen;
	
	char randomAnswer;
	MediaTracker tracker;
	AnimThread anim;
	ArrayList<Puzzle> puzzles;
	int currentPuzzle;
	
	public PuzzlePalooza()
	{
		super();
		
		offscreen = new BufferedImage(800, 600, BufferedImage.TYPE_3BYTE_BGR);
	
		setFocusable(true);
		
		setSize(800, 600);
		
		tracker = new MediaTracker(this);
		
		Toolkit toolkit = Toolkit.getDefaultToolkit();

		//Load images
		option1 = toolkit.getImage("Option1.png");
		option2 = toolkit.getImage("Option2.png");
		option3 = toolkit.getImage("Option3.png");
		option4 = toolkit.getImage("Option4.png");
		
		select1 = toolkit.getImage("Select1.png");
		select2 = toolkit.getImage("Select2.png");
		select3 = toolkit.getImage("Select3.png");
		select4 = toolkit.getImage("Select4.png");
		
		tracker.addImage(option1, 1);
		tracker.addImage(option2, 1);
		tracker.addImage(option3, 1);
		tracker.addImage(option4, 1);
		
		offscreen = new BufferedImage(800, 600, BufferedImage.TYPE_3BYTE_BGR);
		
		readPuzzles();
		
		//enter.addActionListener(new ButtonAction());
		
		addMouseListener(this);
		addMouseMotionListener(this);
		
		try 
		{
			tracker.waitForAll();  // wait for images to fully load
		}
		catch(InterruptedException e)
		{
			System.out.println("Error!");
		}
	}
	
	public void paint(Graphics g)
	{
		update(g);
	}
	
	public void update(Graphics g)
	{
		Graphics2D g2 = (Graphics2D) offscreen.getGraphics();

		g2.setColor(Color.BLACK);
		
		g2.fillRect(0, 0, 800, 600);
		
		//When the mouse is moved over the selection, it changes from the
		//standard Red image to a slightly smaller blue image.
		
		if(currentX > 20 && currentX < 256 + 20 && currentY > 100 && currentY < 100 + 80)
			g2.drawImage(select1, 20 + ((256 - 192)/2), 100 + 10, this);
		else
			g2.drawImage(option1, 20, 100, this);
		
		if(currentX > 20 && currentX < 256 + 20 && currentY > 200 && currentY < 200 + 80)
			g2.drawImage(select2, 20 + ((256 - 192)/2), 200 + 10, this);
		else
			g2.drawImage(option2, 20, 200, this);
		
		if(currentX > 20 && currentX < 256 + 20 && currentY > 300 && currentY < 300 + 80)
			g2.drawImage(select3, 20 + ((256 - 192)/2), 300 + 10, this);
		else
			g2.drawImage(option3, 20, 300, this);
		
		if(currentX > 20 && currentX < 256 + 20 && currentY > 400 && currentY < 400 + 80)
			g2.drawImage(select4, 20 + ((256 - 192)/2), 400 + 10, this);
		else
			g2.drawImage(option4, 20, 400, this);
		
		g2.setColor(Color.RED);
		g2.drawRect(currentX - 10, currentY - 10, 20, 20);
		g2.drawRect(currentX - 5, currentY - 5, 10, 10);
		g2.drawLine(currentX, currentY + 25, currentX, currentY - 25);
		g2.drawLine(currentX + 25, currentY, currentX - 25, currentY);
		
		g.drawImage(offscreen, 0, 0, this);
	}
	
	public void readPuzzles()
	{
		ArrayList<String> temp = new ArrayList<String>();
		try
		{
			Scanner reader = new Scanner(new FileReader("allpuzzles.txt"));
			while(reader.hasNext())
				temp.add(reader.nextLine());
			System.out.println(temp.size());
			int numPuzzle = (int)(Math.random() * (temp.size()));
			String puzzleSetName = temp.get(numPuzzle);
		}
		catch (FileNotFoundException e)
		{
			System.out.println("File not found");
		}
	}
	
	private void answer()
	{
		//System.out.println("--");
		
		int random = (int)(Math.random() * 4 + 1);

		if(random == 1)
			randomAnswer = 'A';	
		else if(random == 2)
			randomAnswer = 'B';
		else if(random == 3)
			randomAnswer = 'C';
		else if(random == 4)
			randomAnswer = 'D';
				
		if(selected == 'A' && randomAnswer == 'A')
		{
			System.out.println("Correct!");
		}
		else if(selected == 'B' && randomAnswer == 'B')
		{
			System.out.println("Correct!");
		}
		else if(selected == 'C' && randomAnswer == 'C')
		{
			System.out.println("Correct!");
		}
		else if(selected == 'D' && randomAnswer == 'D')
		{
			System.out.println("Correct!");
		}
		else if(selected != ' ')
		{
			System.out.println("Incorrect, you failure!!");
		}

		selected = ' ';
	}

	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		currentX = e.getX();
		currentY = e.getY();
		
		//System.out.println("---");
		
		if(currentX > 20 && currentX < 256 + 20 && currentY > 100 && currentY < 100 + 80)
		{
			selected = 'A';
			answer();
		}
		else if(currentX > 20 && currentX < 256 + 20 && currentY > 200 && currentY < 200 + 80)
		{
			selected = 'B';
			answer();
		}
		else if(currentX > 20 && currentX < 256 + 20 && currentY > 300 && currentY < 300 + 80)
		{
			selected = 'C';
			answer();
		}
		else if(currentX > 20 && currentX < 256 + 20 && currentY > 400 && currentY < 400 + 80)
		{
			selected = 'D';
			answer();
		}
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		currentX = e.getX();
		currentY = e.getY();
		//paint(getGraphics());
	}
	
	public void start()
	{
		anim = new AnimThread();
		anim.start();
	}
	
	class AnimThread extends Thread 
	{
		public void run() 
		{
			while (Thread.currentThread() == anim) 
			{
				repaint();
	
				try 
				{
					Thread.sleep(8);
				}
				catch (InterruptedException e)
				{
					System.out.println("Interrupted Exception Error!");
				}
			}
		}
	} 
}